﻿using System;
using System.IO;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Media.Imaging;
using BestCode.Creds.Entities;
using BestCode.Creds.UI.ViewModel;
using NHibernate.Criterion;
using UserControl = System.Windows.Controls.UserControl;

namespace BestCode.Creds.UI
{
    public partial class VwEditFieldDefinition : UserControl
    {
        public event EventHandler OnCloseControl;
        private VmFieldDefinition viewModel;

        private FieldDefinition TempForRestoreCancel;


        public VwEditFieldDefinition(FieldDefinition currentEntity = null)
        {
            InitializeComponent();
            viewModel = new VmFieldDefinition(currentEntity.Entity);
            this.DataContext = viewModel;

            viewModel.SelectedItem = currentEntity;

            TempForRestoreCancel = currentEntity;

            Loaded += VwEditFieldDefinition_Loaded;

        }

        void VwEditFieldDefinition_Loaded(object sender, RoutedEventArgs e)
        {
            Stream stream = null;
            btnPicture.Click += delegate
            {
                OpenFileDialog openFileDialog1 = new OpenFileDialog();
                openFileDialog1.Filter = @"Image Files(*.PNG;*.JPG;*.JPEG;*.BMP;*.GIF)|*.PNG;*.JPG;*.JPEG;*.BMP;*.GIF";


                if (viewModel.SelectedItem.EntityChild != null)
                {
                    openFileDialog1.InitialDirectory = viewModel.SelectedItem.EntityChild.PicturesFolder;
                }
                else
                {
                    openFileDialog1.InitialDirectory = viewModel.SelectedItem.Entity.PicturesFolder;
                }

                openFileDialog1.FilterIndex = 2;
                openFileDialog1.RestoreDirectory = true;
                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    viewModel.SelectedItem.Picture = openFileDialog1.SafeFileName;
                    imgPerson.Source = new BitmapImage(new Uri(viewModel.SelectedItem.Entity.PicturesFolder + "\\" + viewModel.SelectedItem.Picture));
                    //openFileDialog1.SafeFileName.Substring(0, openFileDialog1.SafeFileName.LastIndexOf('.'));
                    //if (stream != null)
                    //    stream.Close();
                    //stream = File.Open(openFileDialog1.FileName, FileMode.Open);
                    //BitmapImage imgsrc = new BitmapImage();
                    //imgsrc.BeginInit();
                    //imgsrc.StreamSource = stream;
                    //imgsrc.EndInit();
                    //imgPerson.Source = imgsrc;
                }
            }; 




            //loading picture
            if (viewModel.SelectedItem != null && !string.IsNullOrEmpty(viewModel.SelectedItem.Picture))
            {
                try
                {

                    if (viewModel.SelectedItem.EntityChild != null)
                    {
                        imgPerson.Source =
                            new BitmapImage(
                                new Uri(viewModel.SelectedItem.EntityChild.PicturesFolder + "\\" +
                                        viewModel.SelectedItem.Picture));
                    }
                    else
                    {
                        imgPerson.Source = new BitmapImage(new Uri(viewModel.SelectedItem.Entity.PicturesFolder + "\\" + viewModel.SelectedItem.Picture));
                    }
                    
                    //if (stream != null)
                    //    stream.Close();
                    //stream = File.Open(viewModel.SelectedItem.Entity.PicturesFolder +"\\"+viewModel.SelectedItem.Picture, FileMode.Open);
                    //BitmapImage imgsrc = new BitmapImage();
                    //imgsrc.BeginInit();
                    //imgsrc.StreamSource = stream;
                    //imgsrc.EndInit();
                    //imgPerson.Source = imgsrc;
                }
                catch (Exception ex)
                {
                   throw new Exception("Picture path not valid!! "+ex.Message);
                }
                
            }
        }


        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            viewModel.SelectedItem = TempForRestoreCancel;
            CloseControl();
        }

        private void CloseControl()
        {
            if (OnCloseControl != null)
                OnCloseControl(this, EventArgs.Empty);
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
                viewModel.Save(viewModel.SelectedItem);
                CloseControl();
        }

        //private void btnPicture_Click(object sender, RoutedEventArgs e)
        //{
        //    OpenFileDialog openFileDialog1 = new OpenFileDialog();
        //    openFileDialog1.InitialDirectory = viewModel.SelectedItem.Entity.PicturesFolder;
        //    openFileDialog1.Filter = "Images files (*.png)|*.png|All files (*.*)|*.*" ;
        //    openFileDialog1.FilterIndex = 2 ;
        //    openFileDialog1.RestoreDirectory = true ;
            
        //    if (openFileDialog1.ShowDialog() == DialogResult.OK)
        //    {
        //        viewModel.SelectedItem.Picture =
        //            openFileDialog1.SafeFileName.Substring(0,openFileDialog1.SafeFileName.LastIndexOf('.'));



        //    }
        //}
    }
}
